﻿using System;
using System.Collections.Generic;
using System.IO;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Lib.Common;
using Lib.Utility;
using Module.BusinessLogic;
using Module.Core.LoginSystem.Commons.Framwork;
using Module.EntitiesInfo;
using ConvertImage = Module.Core.LoginSystem.Commons.Utilities.ConvertImage;
using ConvertUtility = Lib.Utility.ConvertUtility;

namespace Web.Cms.Video
{
    public partial class AddEditVideo : AdminUserControl
    {
        #region Khai báo
        private int videoId;
       private CMSVideoClipInfo info=new CMSVideoClipInfo();
        #endregion

        #region PageLoad
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                videoId = ConvertUtility.ToInt32(Request.QueryString["videoId"]) == null ? 0 : ConvertUtility.ToInt32(Request.QueryString["videoId"]);
               
                txtTeaser.Height = 150;
                txtTeaser.Width = 600;

                txtBody.Height = 300;
                txtBody.Width = 600;
                
                txtTeaser.SetSecurityFilesGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtTeaser.SetSecurityFlashGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtTeaser.SetSecurityGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtTeaser.SetSecurityImageGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtTeaser.SetSecurityMediaGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtTeaser.SetSecurityTemplateGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));

                txtBody.SetSecurityFilesGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtBody.SetSecurityFlashGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtBody.SetSecurityGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtBody.SetSecurityImageGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtBody.SetSecurityMediaGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));
                txtBody.SetSecurityTemplateGalleryPath(Server.MapPath("/Uploads/" + CurrentUser.UserID));

                     videoId = ConvertUtility.ToInt32(Request.QueryString["VideoId"]);
                if (!Page.IsPostBack)
                {
                    txtCreateOn.SelectedDate = DateTime.Now;                    
                    CMSZonesBL.GetDataComboType(dropZoneId, "0", "(" + ModulTypeBL.VideoClip.Type.ToString() + ")");                   
                    LoadData();
                }
            }
            catch (Exception ex)
            {
            }


        }
        #endregion

        #region LoadData
        private void LoadData()
        {
            try
            {
                if (videoId > 0)
                {
                    btnUpdateVideo.Visible = true;
                    btnAddVideo.Visible = false;
                   
                    info = CMSVideoClipBL.GetInfo(2,videoId.ToString(),"");
                    if (info != null)
                    {
                        txtTitle.Text = info.Title;
                        ImgAvatar.ImageUrl = info.Avatar;
                        ImgAvatar.Visible = true;
                        txtCreateOn.SelectedDate = info.CreatedOn;
                        txtTeaser.Text = info.Teaser;
                        txtBody.Text = info.Body;
                        txtAvatar.Text = info.Avatar;
                        txtUrlVideo.Text = info.UrlVideo;
                        dropZoneId.SelectedValue = info.ZoneId.ToString();
                        SelectedCheckBox(info);
                    }
                }
                else
                {
                    btnUpdateVideo.Visible = false;
                    btnAddVideo.Visible = true;
                    ImgAvatar.Visible = true;

                }
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Có lỗi xảy ra Error: " + ex.Message;
            }
        }
        #endregion

       #region FillData
        void FillData(CMSVideoClipInfo info)
        {

            info.VideoId = videoId;
            info.Title = txtTitle.Text;
            info.Teaser = txtTeaser.Text;
            info.Body = txtBody.Text;
            info.Status = 1;
            info.ShowType = ShowType();
            info.HitCount = 0;
            info.CreatedOn = DateTime.Now;
            info.CreatedBy = CurrentUser.Username;
            info.ZoneId = int.Parse(dropZoneId.SelectedValue.ToString());
            info.BigAvatar = txtAvatar.Text.Trim();
            info.UrlVideo = txtUrlVideo.Text.Trim();             
            info.Avatar = txtAvatar.Text.Trim();                                        
        }
        #endregion

        #region thêm mới
        protected void btnAddVideo_Click(object sender, EventArgs e)
        {
            try
            {
               CMSVideoClipInfo info = new CMSVideoClipInfo();
                FillData(info);
                if(txtTitle.Text.Length==0)
                {

                    MessageBox.Show("Bạn chưa nhập tiêu đề video?");
                    txtTitle.Focus();
                    return;
                }
                if(dropZoneId.SelectedIndex==0)
                {
                    MessageBox.Show("Bạn chưa chọn danh mục?");
                    dropZoneId.Focus();
                    return; 
                }
                else
                {
                    int i = CMSVideoClipBL.Insert(info);
                    if (i > 0)
                    {                        
                        //ClearText();
                        Response.Redirect("/cms/Default.aspx?ctrlkey=ListVideoPublic");

                    } 
                }
                
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }
       #endregion

        #region Sửa
        protected void btnUpdateVideo_Click(object sender, EventArgs e)
        {
            try
            {
                CMSVideoClipInfo info = new CMSVideoClipInfo();
                FillData(info);
                if (txtTitle.Text.Length == 0)
                {

                    MessageBox.Show("Bạn chưa nhập tiêu đề video ");
                    txtTitle.Focus();
                    return;
                }
                if (dropZoneId.SelectedIndex == 0)
                {
                    MessageBox.Show("Bạn chưa chọn danh mục ");
                    dropZoneId.Focus();
                    return;
                }
                else
                {
                    CMSVideoClipBL.Update(info);
                    Response.Redirect("/cms/Default.aspx?ctrlkey=ListVideoPublic");
                }
              
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Error: " + ex.Message;
            }
        }
        #endregion
        protected void btnCanelVideo_Click(object sender, EventArgs e)
        {
            Response.Redirect("/cms/Default.aspx?ctrlkey=ListVideoPublic");
        }


        #region showtype
        int ShowType()
        {
            CheckBox[] Boxes = new CheckBox[3];
            Boxes[0] = chTinMoiNhat;
            Boxes[1] = chTinNoiBat;
            Boxes[2] = chTinTieuDiem;
            return ComomFunctions.ReturnStatusCheck(Boxes);
        }
        #endregion

        #region SelectedCheckBox
        void SelectedCheckBox(CMSVideoClipInfo info)
        {
            CheckBox[] Boxes = new CheckBox[3];
            Boxes[0] = chTinMoiNhat;
            Boxes[1] = chTinNoiBat;
            Boxes[2] = chTinTieuDiem;


            chTinMoiNhat.Checked = ComomFunctions.SelectChecked(ObjConstants.MoiNhat, info.ShowType, Boxes);
            chTinNoiBat.Checked = ComomFunctions.SelectChecked(ObjConstants.NoiBat, info.ShowType, Boxes);
            chTinTieuDiem.Checked = ComomFunctions.SelectChecked(ObjConstants.TieuDiem, info.ShowType, Boxes);
        }
        #endregion
     }
}